package cn.tedu.demo.controller;

import cn.tedu.demo.entity.User;
import cn.tedu.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired(required = false)
    private UserMapper mapper;

    @RequestMapping("/select")
    public String select(){
        List<User> list=mapper.select();
        System.out.println(list.toString());
        return list.toString();
    }
    @RequestMapping("/login")
    public int login(@RequestBody User user){
        System.out.println("login");
        User u=mapper.selectByUsername(user.getUsername());
        if (u==null){
            //该用户不存在返回0
            return 0;
        }else {
            if (user.getPassword().equals(u.getPassword())){
                //密码正确返回1
                return 1;
            }
            //密码错误返回2
            return 2;
        }
    }
}
